<?php
namespace app\admin\model;
use frame\libs\BaseModel;

/**
 * 
 */
class NodeModel extends BaseModel
{
    protected $table = "rbac_node";

    /**
     * 获取所有节点信息
     * @return [type] [description]
     */
    public function getNodes()
    {
        return $this->fetchAll("SELECT * FROM $this->table WHERE 1 ORDER BY action");
    }

    /**
     * 根据id获取节点
     * @param [type] $id [description]
     */
    public function getNodeById($id)
    {
        return $this->fetchOne("SELECT * FROM $this->table WHERE id=:id", array('id'=>$id));
    }

    /**
     * 获取节点数量
     * @return [type] [description]
     */
    public function getNodesCount()
    {
        return $this->rowCount("SELECT * FROM $this->table WHERE 1");
    }

    /**
     * 获取所有节点分组信息
     * @return [type] [description]
     */
    public function getNodeGroups()
    {
        return $this->fetchAll("SELECT groups FROM $this->table WHERE 1 GROUP BY groups");
    }

    /**
     * 更新启用状态
     * @param  int $id     [description]
     * @param  int $status [description]
     * @return bool         [description]
     */
    public function updateStatus($data)
    {   
        try {
            $this->beginTransaction();
            $this->update("UPDATE $this->table SET status=:status,update_time=:update_time WHERE id=:id", $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 删除
     * @param  int $id [description]
     * @return bool     [description]
     */
    public function deleteNodeById($id)
    {
        try {
            $this->beginTransaction();
            $this->update("DELETE FROM $this->table WHERE id=:id", array('id'=>$id));
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 新增节点
     * @param  array $data [description]
     * @return bool       [description]
     */
    public function insertNode($data)
    {
        try {
            $this->beginTransaction();
            $this->insert($data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 更新节点
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public function updateNode($data)
    {
        try {
            $this->beginTransaction();
            $sql = "UPDATE $this->table SET title=:title,groups=:groups,action=:action,status=:status,update_time=:update_time WHERE id=:id";
            $this->update($sql, $data);
            $this->commit();
            return true;
        } catch (Exception $e) {
            $this->rollBack();
            return false;
        }
        return false;
    }

    /**
     * 根据节点标识查找节点
     * @param  [type] $action [description]
     * @return [type]         [description]
     */
    public function getNodeByAction($action)
    {
        return $this->fetchOne("SELECT * FROM $this->table WHERE action=:action", array('action'=>$action));
    }

}

